pacman::p_load(
  dplyr,
  scales,
  ggplot2,
  modelsummary,
  knitr,
  kableExtra,
  ggpubr,
  tidyr,
  csvy,
  stargazer
)


# FIGURE 1 ----

read.csv("data/clean/gss_yearmeans.csv") %>%
  mutate(variable = case_when(variable=="helpblk"~"Government has a special obligation to help\nimprove the living standards of Blacks\n(0 = disagree, 1 = agree)",
                              variable=="wrkwayup"~"Blacks should work their way up without special favors\n(0 = agree, 1 = disagree)",
                              variable=="ineqdisc"~"Racial inequality is mainly due to discrimination\n(0 = disagree, 1 = agree)")) %>%
  filter(!is.na(variable)) %>%
  full_join(read.csv("data/clean/anes_yearmeans_blackinf.csv")) %>%
  mutate(variable = case_when(variable=="Blacks have (too much — too little) influence in politics" ~
                                "Blacks have (0 = too much, 1 = too little)\ninfluence in politics",
                              T~variable)) %>%
  ggplot(aes(x = year, y = value, color = group, shape = group)) +
  theme_bw() +
  geom_hline(yintercept = 0.5, linetype = 2, color = "darkgrey") +
  geom_vline(xintercept = 2016, color = "darkgrey") +
  geom_point() +
  geom_line() +
  ylim(c(0,1)) +
  facet_wrap(~factor(variable, levels=c("Racial inequality is mainly due to discrimination\n(0 = disagree, 1 = agree)",
                                        "Blacks should work their way up without special favors\n(0 = agree, 1 = disagree)",
                                        "Government has a special obligation to help\nimprove the living standards of Blacks\n(0 = disagree, 1 = agree)", "Blacks have (0 = too much, 1 = too little)\ninfluence in politics")), ncol = 2, strip.position = "top", scales = "free_x") +
  ylab("") +
  xlab("") +
  scale_color_manual(guide = "none", values = c("grey45", "black", "grey75")) +
  theme(text = element_text(family = "serif"),
        panel.spacing = unit(1, "lines"),
        strip.placement = "outside",
        panel.grid = element_blank(),
        strip.background.y = element_rect(fill = "white"),
        strip.background.x = element_rect(fill = "white"),
        legend.position = "none") +
  xlim(c(1970, 2024)) +
  geom_text(data = data.frame(x = 1970, y = 0.57, variable = "Racial inequality is mainly due to discrimination\n(0 = disagree, 1 = agree)", label = "White Democrats", group = "White Democrat"), aes(x = x, y = y, label = label),  hjust = 0, family = "serif") +
  geom_text(data = data.frame(x = 1970, y = 0.17, variable = "Racial inequality is mainly due to discrimination\n(0 = disagree, 1 = agree)", label = "White Republicans", group = "White Republican"), aes(x = x, y = y, label = label), hjust = 0, family = "serif") +
  geom_text(data = data.frame(x = 1970, y = 0.92, variable = "Racial inequality is mainly due to discrimination\n(0 = disagree, 1 = agree)", label = "Black Democrats", group = "Black Democrat"), aes(x = x, y = y, label = label),  hjust = 0, family = "serif")

ggsave(filename = "fig1.pdf", path = "results", units = "in", width = 6.5, height = 7, dpi=700)

# FIGURE 2 ----
congress <-
  read.csv(
    "data/clean/congress.csv"
  ) %>%
  filter(!is.na(year)) %>%
  mutate(type = case_when(pct_white > 0.5 ~ "Majority-white districts",
                          pct_black > 0.5 ~ "Majority-Black districts",
                          T~"Other majority-minority districts"),
         type = factor(type, levels=c("Majority-Black districts", "Other majority-minority districts", "Majority-white districts"), ordered=T),
         yearnum = (year - 2010)/2,
         majority_dem = dem_vote_share > 0.5)

congress_plot <- congress %>%
  group_by(year, type, black, party) %>%
  mutate(pct_white_blackmc = case_when(black==1~round(pct_white * 100, 0), T~NA_real_),
         pct_dem_blackmc = case_when(black==1~round(dem_vote_share * 100, 0), T~NA_real_)) %>%
  ungroup() %>%
  group_by(year, type, party) %>%
  summarise(black = sum(black, na.rm = T),
            total = n(),
            label = paste(black, "/", total, sep = ""),
            pct_white_blackmc = mean(pct_white_blackmc, na.rm = T),
            pct_dem_blackmc = mean(pct_dem_blackmc, na.rm = T)) %>%
  group_by(year, type) %>%
  mutate(ntotal = sum(total),
         nblack = sum(black),
         pct_black = paste(round(nblack / ntotal, 3) * 100, "%", sep = ""),
         position = nblack + 1) %>%
  ungroup() %>%
  filter(black > 0) %>%
  mutate(party = factor(party, levels = c("Republican", "Democrat"), ordered=T))
  

ggplot(congress_plot, aes(x = year, y = black, fill = party)) +
  geom_bar(stat="identity", color = "black") +
  scale_fill_manual(values = c("white", "grey")) +
  labs(x = "Year", y = "Number of Black MCs") +
  guides(color=guide_legend(title=element_blank())) +
  scale_x_continuous(breaks = seq(2010, 2022, by = 2)) +
  theme_bw() +
  theme(legend.position = "none",
        text = element_text(family = "serif"),
        panel.grid = element_blank(),
        strip.placement = "outside",
        strip.background = element_rect(fill = "white"),
        strip.text.y = element_text(face = "bold")) +
  geom_text(data=subset(congress_plot, black > 0), aes(label = label), size=2.5, position = position_stack(vjust = 0.5), family = 'serif') +
  geom_text(data=subset(congress_plot, party=="Democrat"), aes(label = pct_black, y = position), position = position_stack(vjust = 1), size = 3.5, family = 'serif') +
  facet_grid(type~.) +
  geom_label(data = data_frame(type = factor(c("Majority-white districts"))), aes(x = 2010.5, y = 17, label = "Democratic MCs"), fill = "darkgrey", color = "black", family = "serif") +
  geom_label(data = data_frame(type = factor(c("Majority-white districts"))), aes(x = 2010.5, y = 20, label = "Republican MCs"), fill = "white", color = "black", family = "serif")

ggsave(filename = "fig2.pdf", path = "results", units = "in", width = 6.5, height = 7.5, dpi=700)

# TABLE 2 ----
congress <-
  read.csv(
    "data/clean/congress.csv"
  ) %>%
  filter(!is.na(year)) %>%
  mutate(type = case_when(pct_white > 0.5 ~ "Majority-white districts",
                          pct_black > 0.5 ~ "Majority-Black districts",
                          T~"Other majority-minority districts"),
         type = factor(type, levels=c("Majority-Black districts", "Other majority-minority districts", "Majority-white districts"), ordered=T),
         black = case_when(is.na(black)~0, T~black))

mod1 <- lm(pct_white ~ factor(year), congress[congress$pct_white>0.5,])
mod2 <- lm(black ~ factor(year) + pct_white, congress[congress$pct_white>0.5,])
mod3 <- lm(black ~ factor(year) + pct_white, congress[congress$pct_white<=0.5&congress$pct_black>0.5,])
mod4 <- lm(black ~ factor(year) + pct_white, congress[congress$pct_white<=0.5&congress$pct_black<=0.5,])
tab2 <- stargazer(mod1, mod2, mod3, mod4, type = "latex",
                   dep.var.labels = c("CVAP pct. white", "Black representative"),
                   column.separate = c(1,1,1,2),
                   covariate.labels = c("2012", "2014", "2016", "2018", "2020", "2022", "Pct. white"),
                   header = F,
                   font.size = "small",
                   omit.stat = c("f", "ser", "rsq", "adj.rsq"),
                   column.sep.width = "-3pt", 
                   star.char = c("*", "**", "***"),
                   star.cutoffs = c(.05, .01, .001),
                   label = paste0("tab:congtab"),
                   notes = c("Reference category is 2010. Percent white scaled 0-1."),
                   notes.align = "l",
                   single.row = T,
                    add.lines=list(c('District type', 'Majority-white','Majority-white', 'Majority-Black', 'Other majority-minority')),
                   title = "Majority-white districts citizen voting age percent white over time and proportion of U.S. House districts with a Black Representative by year and type of district, controlling for CVAP percent white")

writeLines(tab2, "results/tab2.tex")

# FIGURE 3 ----

ggplot(congress[congress$year%in%c(2010, 2022),], aes(x = pct_white, y = black, color = factor(year))) +
  geom_rug(data = congress[congress$year%in%c(2010),], sides = "t", length = unit(0.02, "npc"), linewidth = 0.2) +
  geom_rug(data = congress[congress$year%in%c(2022),], sides = "b", length = unit(0.02, "npc"), linewidth = 0.2) +
  geom_vline(xintercept = median(congress[congress$year==2022,]$pct_white), linetype = "dashed", color = "black") +
  geom_vline(xintercept = median(congress[congress$year==2010,]$pct_white), linetype = "dashed", color = "darkgrey") +
  geom_smooth(method = "loess", se = F, span = 1) +
  theme_bw() +
  theme(text = element_text(family = "serif"),
        panel.grid = element_blank(),
        legend.position = "none") +
  coord_cartesian(xlim = c(0, 1), ylim = c(0, 0.75)) +
  xlab("District proportion white") +
  ylab("Estimated proportion of districts with Black MC") +
  scale_color_manual(values = c("darkgrey", "black")) +
  geom_text(y = 0.44, x = 0.025, label = "112th Congress", family = "serif", color = "darkgrey", check_overlap = T, size = 3) +
  geom_text(y = 0.32, x = 0.025, label = "118th Congress", family = "serif", color = "black", check_overlap = T, size = 3) +
  geom_text(y = 0.45, x = median(congress[congress$year==2022,]$pct_white) - 0.015, angle = 90, label = "118th Congress median district proportion white", family = "serif", color = "black", check_overlap = T, size = 3) +
  geom_text(y = 0.45, x = median(congress[congress$year==2010,]$pct_white) - 0.015, angle = 90, label = "112th Congress median district proportion white", family = "serif", color = "darkgrey", check_overlap = T, size = 3)

ggsave(filename = "fig3.pdf", path = "results", units = "in", width = 6.5, height = 5, dpi=700)

# FIGURE 4 ----

# See 0_meta_analysis_figures.R

# FIGURE 5 ----

# See 0_meta_analysis_figures.R

# FIGURE 6 ----

read.csv("data/clean/attitudesfigdat.csv") %>%
  mutate(outcome = factor(outcome,
                          levels = c("Racial resentment",
                                     "Perceived anti-Black discrimination",
                                     "Trump feeling thermometer",
                                     "Biden feeling thermometer"),
                          ordered = T)) %>%
  ggplot(aes(x = value, y = chosen_candidate)) +
  geom_hline(yintercept = 0.5, linetype = 'dashed', color = 'darkgrey') +
  geom_smooth(aes(weight = weight), method = "lm", color = "black") +
  geom_point(aes(x = value, y = groupavg, size = weighted_n), shape = 1, stat ="unique") + 
  theme_bw() + 
  ylim(c(0,1)) +
  theme(text = element_text(family = "serif"),
        panel.grid = element_blank(),
        legend.title = element_blank(),
        strip.placement = "outside",
        strip.background = element_blank(),
        legend.position = "none") +
  ylab("Proportion selecting Black candidate") +
  xlab("") +
  facet_wrap(~outcome, strip.position = "bottom")

ggsave(filename = "fig6.pdf", path = "results", units = "in", width = 6.5, height = 4, dpi=700)

# FIGURE 7 ----
options(scipen = 999)

reparationsfig <- read.csv("data/clean/reparationsfig.csv") %>%
  mutate(stances = gsub("This candidate", "Candidate", stances),
         stances = factor(stances, levels = c("Candidate opposes\nopponent supports",
                                              "Candidate supports\nopponent opposes",
                                              "Both candidates oppose",
                                              "Both candidates support"),
                          ordered = T),
         policy_payreps = factor(policy_payreps, 
                                 levels = c("Respondent supports reparations\nor doesn't know",
                                            "Respondent opposes reparations"),
                                 ordered = T))
reparationsfig_pvals <- read.csv("data/clean/reparationsfig_pvals.csv") %>%
  mutate(stances = gsub("This candidate", "Candidate", stances),
         stances = factor(stances, levels = c("Candidate opposes\nopponent supports",
                                              "Candidate supports\nopponent opposes",
                                              "Both candidates oppose",
                                              "Both candidates support"),
                          ordered = T),
         policy_payreps = factor(policy_payreps, 
                                 levels = c("Respondent supports reparations\nor doesn't know",
                                            "Respondent opposes reparations"),
                                 ordered = T),
         p.signif = case_when(p.signif==""~NA_character_, T~p.signif),
         plab = case_when(is.na(p.signif)~NA_character_, T~paste("p ", plab, sep = "")))

ggplot(reparationsfig, aes(x = race, y = estimate, color = race)) +
  geom_hline(yintercept = 0.5, linetype = "dashed", color = "darkgrey") +
  geom_point(position = position_dodge(.5)) +
  geom_errorbar(aes(ymin = lower, ymax = upper), position = position_dodge(.5), width = 0) +
  theme_bw() +
  scale_color_manual(values = c("black", "darkgrey")) +
  theme(legend.position = "none",
        text = element_text(family = "serif"),
        axis.ticks.x = element_blank(),
        panel.grid = element_blank(),
        strip.background = element_rect(fill = "white"),
        strip.text = element_text(family = "serif"),
        axis.text.x = element_blank()) +
  facet_grid(policy_payreps~stances) +
  xlab("Candidate race") +
  ylab("Proportion selecting candidate") + 
  stat_pvalue_manual(reparationsfig_pvals, label = "plab", family = "serif", size = 3) +
  ylim(c(0.1, 0.95)) +
  xlab("") +
  geom_text(data=data.frame(stances = factor(c("Candidate opposes\nopponent supports")), policy_payreps = factor(c("Respondent supports reparations\nor doesn't know"))), y = 0.9, x = 0.5, label = "Black candidates", hjust = 0, color = "black", size = 3, family = "serif", check_overlap = T) +
  geom_text(data=data.frame(stances = factor(c("Candidate opposes\nopponent supports")), policy_payreps = factor(c("Respondent supports reparations\nor doesn't know"))), y = 0.85, x = 0.5, label = "White candidates", hjust = 0, color = "darkgrey", size = 3, family = "serif", check_overlap = T)

ggsave(filename = "fig7.pdf", path = "results", units = "in", width = 6.5, height = 5, dpi=700)

# FIGURE 8 ----

mods <- read.csv("data/clean/ideo_mods.csv")
modsp <- read.csv("data/clean/ideo_modsp.csv") %>%
  mutate(p.signif = case_when(p.signif==""~NA_character_,
                              T~p.signif),
         plab = case_when(is.na(p.signif)~NA_character_, T~paste("p ", plab, sep = "")))

ggplot(mods, aes(x = term, y = estimate))+
  geom_hline(yintercept = 0.5, linetype = "dashed", color = "darkgrey") +
  geom_vline(xintercept = 0, linetype = "dashed", color = "darkgrey") +
  geom_point(aes(color = model), position = position_dodge(.1)) +
  scale_color_manual(values = c("black", "darkgrey")) +
  geom_errorbar(aes(ymin = conf.low, ymax = conf.high, color = model), width = 0, position = position_dodge(.1)) +
  theme_bw() +
  theme(text = element_text(family = "serif"),
        axis.ticks.x = element_blank(),
        panel.grid = element_blank(),
        legend.position = "none") +
  ylab("Proportion selecting candidate") +
  xlab("") +
  scale_y_continuous(limits = c(-0.105, 1.22), breaks = c(0, 0.25, 0.5, 0.75, 1)) +
  scale_x_continuous(limits = c(-1.05, 1.05), breaks = c(-0.67, 0, 0.67), labels = c("Other candidate\nmore congruent", "Candidates equally\ncongruent", "This candidate\nmore congruent")) +
  geom_text(aes(y = 1.15, x = -1, label = "Black candidates"), hjust = 0, color = "black", family = "serif", check_overlap = T) +
  geom_text(aes(y = 1.05, x = -1, label = "White candidates"), hjust = 0, color = "darkgrey", family = "serif", check_overlap = T) + 
  stat_pvalue_manual(modsp, label = "plab", family = "serif", size = 3)

ggsave(filename = "fig8.pdf", path = "results", units = "in", width = 6.5, height = 4, dpi=700)
